package com.upeoe.lucrocrawler.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ven_hxz
 * @create 2016-05-25 上午10:49
 */
public class DateKit {

    public static final String SHORT_DATE = "yyyyMMdd";

    public static final String LONG_DATE = "yyyyMMddHHmmss";

    public static final String LONG_DATE_SEPARATE = "yyyy-MM-dd";

    public static final String DB_DATE = "yyyy-MM-dd HH:mm:ss";

    public static final int MILLS_PER_DAY = 24 * 3600 * 1000;

    private static Map<String, Object> parsers = new HashMap<>();

    private static Logger logger = LoggerFactory.getLogger(DateKit.class);

    /**
     * java.util.Date 转为 String
     *
     * @param date
     * @return
     */
    public static String Date2Str(java.util.Date date) {
        return DateParser(LONG_DATE_SEPARATE).format(date);
    }

    /**
     * 字符串转为数据库日期
     * <p>
     * string to java.sql.Timestamp
     *
     * @param date 字符串日期
     * @return
     */
    public static java.sql.Timestamp Str2Timestamp(String date) {
        return new java.sql.Timestamp(Str2UtilDate(date).getTime());
    }

    /**
     * 字符串转为Date日期
     * <p>
     * string to java.util.Date
     *
     * @param date 字符串日期
     * @return
     */
    public static java.util.Date Str2UtilDate(String date) {
        try {
            return DateParser(DB_DATE).parse(date);
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
        return null;
    }

    /**
     * java.sql.Timestamp 转 java.util.Date
     *
     * @param time
     * @return
     */
    public static java.util.Date Timestamp2Date(java.sql.Timestamp time) {
        return new java.util.Date(time.getTime());
    }

    /**
     * java.util.Date 转 java.sql.Timestamp
     *
     * @param date
     * @return
     */
    public static java.sql.Timestamp Date2Timestamp(Date date) {
        return new java.sql.Timestamp(date.getTime());
    }

    /**
     * String 转 java.util.Date
     *
     * @param dateStr
     * @return
     */
    public static java.util.Date Str2Date(String dateStr) {
        try {
            return DateParser(DB_DATE).parse(dateStr);
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
        return null;
    }

    /**
     * 给定日期,后移N天
     *
     * @param date 给定的日期
     * @param day  向后移动的天数
     * @return
     */
    public static java.util.Date addDate(java.util.Date date, int day) {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.setTimeInMillis(getMills(date) + ((long) day) * MILLS_PER_DAY);
        return calendar.getTime();
    }

    /**
     * 给定日期,前移N天
     *
     * @param date 传入的日期
     * @param day  向前移动的天数
     * @return
     */
    public static java.util.Date subDate(java.util.Date date, int day) {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.setTimeInMillis(getMills(date) - ((long) day) * MILLS_PER_DAY);
        return calendar.getTime();
    }

    /**
     * 获取日期的毫秒值
     *
     * @param date 日期
     * @return
     */
    public static long getMills(java.util.Date date) {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

    /**
     * 比较两个日期相差天数
     *
     * @param behindDate  较早的一天
     * @param afterDate   较后的一天
     * @param datePattern 日期格式
     * @return
     * @throws ParseException
     */
    public static int daysDiff(java.util.Date behindDate, java.util.Date afterDate, String datePattern) throws ParseException {
        SimpleDateFormat sdf = DateParser(datePattern);
        behindDate = sdf.parse(sdf.format(behindDate));
        afterDate = sdf.parse(sdf.format(afterDate));

        java.util.Calendar calendar = Calendar.getInstance();

        calendar.setTime(behindDate);
        long behindTimes = calendar.getTimeInMillis();
        calendar.setTime(afterDate);
        long afterTimes = calendar.getTimeInMillis();

        return Integer.parseInt(String.valueOf((afterTimes - behindTimes) / MILLS_PER_DAY));
    }

    /**
     * 判断两个日期是否为同一天
     *
     * @param var1 日期一
     * @param var2 日期二
     * @return
     */
    public static boolean isSameDay(java.util.Date var1, java.util.Date var2) {
        SimpleDateFormat sdf = DateParser(LONG_DATE_SEPARATE);
        String firstDateStr = sdf.format(var1);
        String secondDateStr = sdf.format(var2);
        return firstDateStr.equals(secondDateStr);
    }

    /**
     * 获取昨天的日期
     *
     * @return
     */
    public static java.util.Date getYesterdayDate() {
        java.util.Date today = new java.util.Date();
        java.util.Date yesterday = subDate(today, 1);
        return yesterday;
    }

    /**
     * 日期格式转换器
     *
     * @param datePattern 日期模板
     * @return
     */
    private static SimpleDateFormat DateParser(String datePattern) {
        Object parser = parsers.get(datePattern);

        if (parser == null) {
            parser = new SimpleDateFormat(datePattern);
            parsers.put(datePattern, parser);
        }

        return (SimpleDateFormat) parser;
    }
}
